package com.zzxx.controller;

import com.github.pagehelper.PageInfo;
import com.zzxx.entity.*;
import com.zzxx.exceptions.UpdateException;
import com.zzxx.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired(required = false)
    private HttpServletRequest request;
    @RequestMapping("queryAll")
    public List<Activity> queryAll(){
        return activityService.queryAll();
    }

    @RequestMapping("queryById")
    public Activity findById(@RequestParam String id){
        return activityService.queryById(id);
    }
    @RequestMapping("queryByUserId")
    public Activity findByUserId(@RequestParam String id){

        return activityService.queryById(id);
    }

    @RequestMapping(value = "orderInfo/{aid}", method = RequestMethod.GET)
    public UserAndCoupons orderInfo(@PathVariable("aid") String aid){
        UserAndCoupons uac = new UserAndCoupons();
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (aid==null||loginUser==null) return null;
        uac.setLoginUser(loginUser);
        activityService.packageUac(uac,aid);
        return uac;
    }

    @RequestMapping(value = "findPage",method = RequestMethod.GET)
    public PageInfo<Activity> findPage(Integer pageNum, Integer pageSize){
        pageNum = pageNum==null? 1:pageNum;
        pageSize = pageSize==null? 5:pageSize;
        PageInfo<Activity> pageInfo= activityService.findPage(pageNum, pageSize);
        return pageInfo;
    }

    @RequestMapping("updateActivity")
    public String updateById(MultipartFile uploadPic, Activity activity) throws IOException {
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return "noLogin";
        try {
            activityService.updateById(uploadPic, activity, loginManager);
        } catch (UpdateException e) {
            return e.getMessage();
        }
        return "success";
    }

    @RequestMapping(value = "addActivity", method = RequestMethod.POST)
    public String addActivity(MultipartFile uploadPic,Activity activity) throws IOException {
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return "noLogin";
        activity.setStatus(0);
        try {
            activityService.addActivity(uploadPic, activity,loginManager);
        } catch (UpdateException e) {
            return e.getMessage();
        }
        return "success";
    }

    @RequestMapping(value = "delete/{aid}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("aid") String aid){
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return "noLogin";
        try {
            activityService.delete(aid,loginManager.getId());
        } catch (UpdateException e) {
            return e.getMessage();
        }
        return "success";
    }

    @RequestMapping(value = "detail/{aid}", method = RequestMethod.GET)
    public ActivityAndRule detail(@PathVariable("aid") String aid){
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return null;
        ActivityAndRule aar = activityService.findAar(loginManager.getId(), aid);
        return aar;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@RequestBody Map<String,ActivityAndRule> map){
//        TicketAndRule tar = JSON.parseObject(JSON.toJSONString(map.get("ticketAndRule")), TicketAndRule.class);
        ActivityAndRule aar = map.get("activityAndRule");
        if (aar==null) return "程序错误，请重新输入";
        if (aar.getRule().getStarttime().after(aar.getRule().getEndtime())) return "开始时间不能晚于结束时间";
        activityService.updateAar(aar);
        return "success";
    }

    @RequestMapping(value = "findPower/{aid}", method = RequestMethod.GET)
    public List<ManagerAndPower> findPower(@PathVariable("aid") String aid){
        //检查有没有权限
        Manager loginManager = (Manager) request.getSession().getAttribute("loginManager");
        if (loginManager==null) return null;
        if (!activityService.checkPower(aid,loginManager.getId())) return null;
        List<ManagerPower> list = activityService.findPower(aid,loginManager.getId());
        List<ManagerAndPower> mapList = activityService.packageMap(list);
        return mapList;
    }

    @RequestMapping("deleteNum")
    public String deleteNum(String id,String num){
        activityService.deleteNum(id,num);
        return "ok";
    }

    @RequestMapping("tui")
    public Boolean tui(String id,String time){
        Boolean flag= null;
        try {
            flag = activityService.tui(id,time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
